package logobj.utils.db;

import logobj.config.ConfProperties;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBCon {
    private static Connection con = null;

    public static void connection(String driver, String url, String dbuser, String dbpwd)  {
        // 加载数据库驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(url,dbuser ,dbpwd );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection(){
        if(con == null){
            connection(ConfProperties.driver,ConfProperties.url,ConfProperties.dbuser,ConfProperties.dbpwd);
        }
        return con;
    }
    //断开链接
    public static void CloseCon(){
        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
